package com.itheima.test;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;
import java.util.Map;

//测试  条件查询
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class UserMapperTest3 {

    @Autowired
    private UserMapper mapper;


    //查询返回指定字段
    //select  name,age form user order by age asc
    @Test
    public void testSelect(){

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();

        wrapper.select(User::getName,User::getAge);//select name,age
        wrapper.orderByDesc(User::getAge);//order by age desc

        List<User> users = mapper.selectList(wrapper);

        for (User user : users) {
            System.out.println(user);
        }


    }

    //分组统计
    //select name,count(*) as count from user group by name having count > 1
    @Test
    public void testSelect2(){
        QueryWrapper<User>  wrapper = new QueryWrapper<>();
        wrapper.select("name", "count(*) as count");
        wrapper.groupBy("name");
        wrapper.having("count>1");
        List<Map<String, Object>> maps = mapper.selectMaps(wrapper);
        System.out.println(maps);
    }

    @Test
    public  void testFindPage(){
        //1.创建page对象，设置分页两个参数,pageNum,pageSize
        Page<User> page = new Page<>(2,3);//第1页，每页3条

        //2.设置查询条件
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.gt(User::getAge,1);

        //3. 执行分页查询
        page = mapper.selectPage(page, wrapper);

        //4. 从返回的page对象中,可以读取用到5个参数
        System.out.println("总条数：" + page.getTotal());
        System.out.println("总页数：" + page.getPages());
        System.out.println("当前页数据：" + page.getRecords());
        System.out.println("当前页码值：" + page.getCurrent());
        System.out.println("每页显示数：" + page.getSize());


    }




}

























